﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TD
{
    /// <summary>
    /// 防御塔
    /// </summary>
    public class StockadeHelper : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game DefaultGame;
        Texture2D defaultTexture;
        Texture2D defaultAttackTexture;
        public StockadeHelper(Game game)
            : base(game) {
            DefaultGame = game;
            TDGame.Stockades.Add(new Stockade { Attack = new Attack { Damage = 10 }, AttackInterval = 200, Range = 200, Vector = new Vector2 { X = 300, Y = 100 } });
        }


        public override void Update(GameTime gameTime) {


            foreach (var s in TDGame.Stockades) {
                s.timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                if (s.timeSinceLastFrame > s.AttackInterval) {
                    s.timeSinceLastFrame -= s.AttackInterval;
                    s.AttackPointList.Clear();
                    foreach (var item in TDGame.Monsters) {
                        if (Vector2.Distance(s.Vector, item.Vector) < s.Range) {
                            item.Rev(s.Attack);
                            s.AttackPointList.Add(item.Vector);
                            break;
                        }
                    }
                }
            }

            base.Update(gameTime);
        }

        protected override void LoadContent() {
            defaultTexture = DefaultGame.Content.Load<Texture2D>("Stockade/ta");
            defaultAttackTexture = DefaultGame.Content.Load<Texture2D>("Stockade/hp");
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime) {
            TDGame.spriteBatch.Begin();
            foreach (var item in TDGame.Stockades) {
                TDGame.spriteBatch.Draw(defaultTexture, item.Vector, Color.White);
                foreach (var v in item.AttackPointList) {
                    TDGame.spriteBatch.Draw(defaultAttackTexture, v, Color.White);
                }

            }
            TDGame.spriteBatch.End();

            //  base.Draw(gameTime);
        }
    }

}